/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Alarma;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class AlarmaDaoImpl extends HibernateDao<Alarma> implements AlarmaDao<Alarma>{
    
    private final String  CLASE_ALARMA = "[AlarmaDaoImpl]";
    
    @Override
    public DetachedCriteria getAlarmas() {
        Timestamp timestamp = Utilitario.getFechaActual();
        DetachedCriteria criteria = DetachedCriteria.forClass(Alarma.class,"alarma").createAlias("alarma.evento","evn").add(Restrictions.ge("evn.fechaEvento",timestamp));
        return criteria;
    } 

    @Override
    public Alarma getAlarma(int idAlarma) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Alarma.class,"alarma").createAlias("alarma.evento","evn").add(Restrictions.eq("alarma.idAlarma", idAlarma));
            return (Alarma)criteria.uniqueResult();
        }catch(HibernateException e){
            Utilitario.LOG.error("Error en AlarmaDaoImpl", e);
            throw  new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public List<Alarma> notificarAlarmas() {
        Session session = null;
        List<Alarma> alarmas = null;
        Timestamp timestamp = Utilitario.getFechaActual();
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Alarma.class,"alarma").createAlias("alarma.evento","evn")
                    .add(Restrictions.eq("alarma.activo",Boolean.TRUE)).add(Restrictions.ge("evn.fechaEvento",timestamp))
                    .addOrder(Order.asc("evn.fechaEvento"));
            alarmas = criteria.list();
            return alarmas;
        }catch(HibernateException e){
            Utilitario.LOG.error("Error en AlarmaDaoImpl", e);
            throw  new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }
    
}
